package com.fsnbigdata.repository;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Product;
import com.fsnbigdata.domain.QProduct;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * = ProductRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = ProductRepositoryCustom.class)
@Transactional(readOnly = true)
public class ProductRepositoryImpl extends QueryDslRepositorySupportExt<Product> implements ProductRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    ProductRepositoryImpl() {
        super(Product.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String TARGET_GROUP = "targetGroup";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String DESCRIPTION = "description";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY = "category";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATOR = "creator";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String FEATURE = "feature";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String IS_EXPORT = "isExport";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String BARCODE = "barcode";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NUTRITION = "nutrition";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String SPECIFICATION = "specification";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String INGREDIENTS = "ingredients";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String BRAND = "brand";
    
    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String TYPE = "type";

    @Override
    public Page<Product> findAll(Predicate predicate, Pageable pageable) {
        QProduct product = QProduct.product;
        JPQLQuery<Product> query = from(product).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        applyPagination(pageable, query);
        return loadPage(query, pageable, product);
    }

    @Override
    public Page<Product> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        QProduct product = QProduct.product;
        JPQLQuery<Product> query = from(product);
        if(creator != null ){
            query.where(product.creator.eq(creator));
        }
        Path<?>[] paths = new Path<?>[] { product.name, product.barcode, product.brand, product.category, product.ingredients, product.targetGroup, product.nutrition, product.feature, product.description, product.specification, product.isExport, product.creator,product.type };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(NAME, product.name).map(BARCODE, product.barcode).map(BRAND, product.brand).map(CATEGORY, product.category).map(INGREDIENTS, product.ingredients).map(TARGET_GROUP, product.targetGroup).map(NUTRITION, product.nutrition).map(FEATURE, product.feature).map(DESCRIPTION, product.description).map(SPECIFICATION, product.specification).map(IS_EXPORT, product.isExport).map(CREATOR, product.creator).map(TYPE,product.type);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, product);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    @Override
    public Page<Product> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        QProduct product = QProduct.product;
        JPQLQuery<Product> query = from(product);
        Path<?>[] paths = new Path<?>[] { product.name, product.barcode, product.brand, product.category, product.ingredients, product.targetGroup, product.nutrition, product.feature, product.description, product.specification, product.isExport, product.creator ,product.type};
        applyGlobalSearch(globalSearch, query, paths);
        // Also, filter by the provided ids
        query.where(product.id.in(ids));
        AttributeMappingBuilder mapping = buildMapper().map(NAME, product.name).map(BARCODE, product.barcode).map(BRAND, product.brand).map(CATEGORY, product.category).map(INGREDIENTS, product.ingredients).map(TARGET_GROUP, product.targetGroup).map(NUTRITION, product.nutrition).map(FEATURE, product.feature).map(DESCRIPTION, product.description).map(SPECIFICATION, product.specification).map(IS_EXPORT, product.isExport).map(CREATOR, product.creator).map(TYPE,product.type);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, product);
    }
}
